package com.jdbc.DAO;

import com.jdbc.Conn;
import com.model.TeacherInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by NULL on 2017/12/14.
 */
public class TeacherInfoServer {

    private Connection conn = Conn.getConnection();
    private PreparedStatement ps;
    public boolean addTeacher(TeacherInfo teacherInfo){
        final String sql= "insert into teacherinfo values(null,?,?,?,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, teacherInfo.getTeacher_name());
            ps.setInt(2, teacherInfo.getTeacher_sex());
            ps.setString(3, teacherInfo.getTeacher_birthday());
            ps.setString(4, teacherInfo.getTeacher_course());
            ps.setString(5, teacherInfo.getTeacher_beizhu());
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    public List<TeacherInfo> queryAllTeacher(){
        List<TeacherInfo> allTeacher = new ArrayList<TeacherInfo>();
        final String sql = "select * from teacherinfo";
        try {
            ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                TeacherInfo s = new TeacherInfo();
                s.setTeacher_id(rs.getInt("teacher_id"));
                s.setTeacher_name(rs.getString("teacher_name"));
                s.setTeacher_sex(rs.getInt("teacher_sex"));
                if(rs.getDate("teacher_birthday") != null)
                    s.setTeacher_birthday(rs.getDate("teacher_birthday").toString());
                if(rs.getString("teacher_course") != null)
                    s.setTeacherCourseArray(rs.getString("teacher_course").split(","));
                allTeacher.add(s);
            }
            return allTeacher;

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public TeacherInfo queryTeacherById(int teacher_id){
        TeacherInfo t = new TeacherInfo();
        final String sql = "select * from teacherInfo where teacher_id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,teacher_id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                t.setTeacher_id(rs.getInt("teacher_id"));
                t.setTeacher_name(rs.getString("teacher_name"));
                t.setTeacher_sex(rs.getInt("teacher_sex"));
                if(rs.getDate("teacher_birthday") != null)
                    t.setTeacher_birthday(rs.getDate("teacher_birthday").toString());
                if(rs.getString("teacher_course") != null)
                    t.setTeacherCourseArray(rs.getString("teacher_course").split(","));
                t.setTeacher_beizhu(rs.getString("teacher_beizhu"));
                return t;
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }


    public boolean updateTeacher(TeacherInfo teacherInfo){
        final String sql = "update teacherinfo set teacher_name=?,teacher_sex=?,teacher_birthday=?,teacher_course=?,"
                +"beizhu=? where teacher_id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, teacherInfo.getTeacher_name());
            ps.setInt(2, teacherInfo.getTeacher_sex());
            ps.setString(3, teacherInfo.getTeacher_birthday());
            ps.setString(4, teacherInfo.getTeacher_course());
            ps.setString(5, teacherInfo.getTeacher_beizhu());
            ps.setInt(6, teacherInfo.getTeacher_id());
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean deleteTeacher(int teacher_id){
        final String sql = "delete from teacherinfo where teacher_id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, teacher_id);
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

}
